/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.scribeplugin.csv;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.pearreview.application.Messages;
import com.pearreview.util.review.Finding;

public class FindingCustomColumn implements FindingModelColumnRenderer,
		Serializable {
	private static final long serialVersionUID = -7600858663305421049L;
	public static final String FIELD_NAME = Messages.getString(
			"PearReview.Scribe.trac_settings.own_colomn");
	private String name = FIELD_NAME;
	private String defaultValue = "";
	private Map<Finding, String> valueMap = new HashMap<Finding, String>();
	public FindingCustomColumn() {
		
	}
	public FindingCustomColumn(String value) {
		this.setDefaultValue(value);
	}
	public Map<Finding, String> getValueMap() {
		return valueMap;
	}
	@Override
	public String getColumnName() {
		return name + " [" + FIELD_NAME + "]";
	}
	@Override
	public String getValue(Finding finding) {
		if(valueMap == null || !valueMap.containsKey(finding)) {
			return getDefaultValue();
		} else {
			return valueMap.get(finding);
		}
	}
	@Override
	public Class<?> getValueClass() {
		return String.class;
	}
	@Override
	public boolean isEditable() {
		return true;
	}
	@Override
	public void setName(String newName) {
		name = newName;
	}
	@Override
	public String getName() {
		return name;
	}
	@Override
	public void setNewValue(Finding finding, Object newValue) {
		if(!getDefaultValue().equals(newValue))
			valueMap.put(finding, newValue.toString());
	}
	@Override
	public String getStringValue(Finding finding) {
		return getValue(finding);
	}
	public void setDefaultValue(String defaultValue) {
		if(defaultValue == null)
			defaultValue = "";
		
		for(Finding finding: valueMap.keySet()) {
			if(defaultValue.equals(valueMap.get(finding)))
				valueMap.remove(finding);
		}
		
		this.defaultValue = defaultValue;
	}
	public String getDefaultValue() {
		return defaultValue;
	}
}